/*
 * shared.h
 *
 *  Created on: 2012-01-05
 *      Author: mateusz
 */

#ifndef SHARED_H_
#define SHARED_H_

#include <sys/sem.h>

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define CLIENTS_COUNT 2
#define SERVER_QUEUE 12345	// public server queue
#define MAX_LOG_LENGTH 80

enum logMessages {
	LOG_SERVER_DOES_NOT_EXIST = 1, LOG_HELP, LOG_WAITING_FOR_ALL_PLAYERS, LOG_WINDOW_TITLE, // maybe to change
	STATS_WINDOW_TITLE,
	COMMAND_WINDOW_TITLE,
	LOG_WELCOME_MESSAGE,
	LOG_ENTITIES_WILL_BE_CREATED,
	LOG_NOT_ENOUGH_SUPPLIES_TO_CREATE_ENTITIES,
	LOG_WIN_EXIT,
	LOG_EXIT,
	LOG_NOT_ENOUGH_UNITS_TO_MAKE_ATTACK,
	LOG_MAKING_ATTACK,
	LOG_WAS_DEFENSE,
	LOG_WAS_ATTACK
};

enum clientServerMessages {
	CS_COMMAND = 1,
	CS_LOG,
	CS_STATS,
	CS_INIT,
	CS_BATTLE,
	CS_BATTLE_END,
	CS_CLIENT_START,
	CS_CLIENT_PROCESS_READY,
	CS_PRIVATE_SERVER_QUEUE_READY,
	CS_SERVER_START,
	CS_ATTACK_PROCESS_CLOSED,
	CS_EXIT,
	CS_LAST_MSG = CS_EXIT,
	CS_EXIT_CONFIRMED = 20
};
enum entities {
	SUPPLIES = 0,
	WORKERS,
	LIGHT_INFANTRY,
	HEAVY_INFANTRY,
	CAVALRY,
	FIRST_ENTITY = SUPPLIES,
	LAST_ENTITY = CAVALRY,
	FIRST_ATTACKED_ENTITY = LIGHT_INFANTRY
};

enum commands {
	COMMAND_ATTACK = 1, COMMAND_CREATE_ENTITIES, COMMAND_WAS_BATTLE, COMMAND_WAS_ATTACK
};

// client-server messages
struct intMessage {
	long type;
	int mtext;
};
struct intMessage intMsg;

struct entitiesMessage {
	long type;
	int units[LAST_ENTITY + 1];
	int wins;
};
struct entitiesMessage entitiesMsg;

struct logMessage {
	long type;
	int logID;
	int params[LAST_ENTITY + 1];
};
struct logMessage logMsg;

struct commandMessage {
	long type;
	int commandId;
	int params[LAST_ENTITY + 1]; // każdy parametr odpowiada jednoste o indeksie z enuma - będzie to liczba tworzonych lub liczba jednostek wysyłanych do ataku
};
struct commandMessage commandMsg;

int getQueue(int id, int flags, int reset);

int sendIntMsg(int qID, long type, int msg);
int getIntMsg(int qID, long type);

int sendLogMsg(int qID, long type);
int getLogMsg(int qID, long type);

int sendEntitiesMsg(int qID, long type);
int getEntitiesMsg(int qID, long type);

int sendCommandMsg(int qID, long type);
int getCommandMsg(int qID, long type);

// semaphores
struct sembuf sem;

void semOperation(int semid, int val);
void semDown(int semid);
void semUp(int semid);
int semValue(int semid);
int newSem(int id, int value);
int getSem(int id);

// shm
int newShm(int id, int size);
int getShm(int id, int size);
#endif /* SHARED_H_ */
